package com.dhwl.hsh.driver.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.dhwl.hsh.driver.R;
import com.dhwl.hsh.driver.common.Constant;
import com.dhwl.hsh.driver.dto.Address;
import com.dhwl.hsh.driver.ui.adapter.TaskAddressAdapter;
import com.dhwl.hsh.driver.ui.dialog.BillDialogFragment;
import com.dhwl.hsh.driver.view.ScrollViewWithListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnItemClick;

/**
 * 我要用车页面
 *
 * @author Derek.Kidd
 * @time 2016/4/25 10:00
 */
public class AddTaskActivity extends BaseActivity implements BillDialogFragment.BillInputListener{

    @Bind(R.id.scrollView_content)
    ScrollView scrollView;

    @Bind(R.id.listView_address)
    ScrollViewWithListView addressListView;

    @Bind(R.id.tv_transport_date)
    TextView tvTransportDate;

    @Bind(R.id.tv_transport_time_des)
    TextView tvTransportTimeDes;

    @Bind(R.id.layout_transport_time)
    RelativeLayout layoutTransportT;

    @Bind(R.id.btn_add_shipping_add)
    Button btnAddShippingAdd;

    @Bind(R.id.tv_choose_truck)
    TextView tvChooseTruck;
    @Bind(R.id.arrow_choose_car)
    ImageView arrowChooseCar;
    @Bind(R.id.layout_choose_car)
    RelativeLayout layoutChooseCar;

    @Bind(R.id.tv_bill_type)
    TextView tvBillType;
    @Bind(R.id.arrow_bill_type)
    ImageView arrowBillType;
    @Bind(R.id.layout_bill_type)
    RelativeLayout layoutBillType;
    @Bind(R.id.icon_extra_service)
    ImageView iconExtraService;
    @Bind(R.id.tv_extra_service)
    TextView tvExtraService;

    @Bind(R.id.btn_need_carry)
    Button btnNeedCarry;
    @Bind(R.id.btn_need_receipt)
    Button btnNeedReceipt;
    @Bind(R.id.btn_need_remittance)
    Button btnNeedRemittance;

    @Bind(R.id.tv_tip_for_driver)
    TextView tvTipForDriver;

    @Bind(R.id.layout_tip_for_driver)
    RelativeLayout layoutTipForDriver;


    @Bind(R.id.tv_words_for_driver)
    TextView tvWordsForDriver;

    @Bind(R.id.layout_words_for_driver)
    RelativeLayout layoutWordsForDriver;


    private List<Address> addressList;
    private TaskAddressAdapter taskAddressAdapter;

    /**
     * 计费方式
     */
    private int billType = Constant.Order.VALUE_BILL_TYPE_1;
    /**
     * 是否需要搬运
     */
    private boolean isNeedCarry = false;
    /**
     * 是否需要回单
     */
    private boolean isNeedReceipt = false;
    /**
     * 是否需要汇款
     */
    private boolean isNeedRemittance = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_task;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setTitleContent(R.string.title_add_task);
        addressListView.setParentScrollView(scrollView);
    }

    @Override
    protected void initData() {

        addressList = new ArrayList<>();
        //初始化一个空的发货地和配送地
        Address startAdd = new Address();
        startAdd.setAddressType(Constant.Order.VALUE_START_ADDRESS);
        Address endAdd = new Address();
        endAdd.setAddressType(Constant.Order.VALUE_END_ADDRESS);

        addressList.add(0, startAdd);
        addressList.add(1, endAdd);

        taskAddressAdapter = new TaskAddressAdapter(this, addressList);
        addressListView.setAdapter(taskAddressAdapter);

        switch (billType) {
            case Constant.Order.VALUE_BILL_TYPE_1:
                tvBillType.setText(R.string.tv_bill_type_1);
                break;
            case Constant.Order.VALUE_BILL_TYPE_2:
                tvBillType.setText(R.string.tv_bill_type_2);
                break;
            case Constant.Order.VALUE_BILL_TYPE_3:
                tvBillType.setText(R.string.tv_bill_type_1);
                break;
            case Constant.Order.VALUE_BILL_TYPE_4:
                tvBillType.setText(R.string.tv_bill_type_1);
                break;
        }
    }


    @OnItemClick(R.id.listView_address)
    public void onAddressClick(int position) {
        Intent intent = new Intent(this, AddAddressActivity.class);
        intent.putExtra(Constant.Order.KEY_SHIPPING_ADDRESS, (Parcelable) addressList.get(position));
        intent.putExtra("position", position);
        intent.putExtra(Constant.UserInfo.KEY_BILL_TYPE, billType);
        startActivityForResult(intent, Constant.Order.REQUEST_CODE_2);
    }


    @OnClick({R.id.layout_transport_time, R.id.btn_add_shipping_add, R.id.layout_choose_car, R.id.btn_need_carry, R.id.btn_need_receipt, R.id.btn_need_remittance, R.id.layout_tip_for_driver, R.id.layout_words_for_driver})
    public void onClick(View view) {

        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.layout_transport_time:
                intent.setClass(this, ChooseTimeActivity.class);
                startActivityForResult(intent, Constant.Order.REQUEST_CODE_1);
                break;
            case R.id.btn_add_shipping_add:
                intent.setClass(this, AddAddressActivity.class);
                //新增空的配送地对象
                Address endAdd = new Address();
                endAdd.setAddressType(Constant.Order.VALUE_END_ADDRESS);
                intent.putExtra(Constant.Order.KEY_SHIPPING_ADDRESS, (Parcelable) endAdd);
                intent.putExtra(Constant.UserInfo.KEY_BILL_TYPE, billType);
                startActivityForResult(intent, Constant.Order.REQUEST_CODE_3);
                break;
            case R.id.layout_choose_car:
                intent.setClass(this, TruckListActivity.class);
                startActivityForResult(intent, Constant.Order.REQUEST_CODE_4);
                break;
            case R.id.layout_bill_type:
                break;
            case R.id.btn_need_carry:
                isNeedCarry = !isNeedCarry;
                if (isNeedCarry) {
                    btnNeedCarry.setBackgroundResource(R.drawable.bg_extra_service_checked);
                } else {
                    btnNeedCarry.setBackgroundResource(R.drawable.bg_extra_service_normal);
                }
                break;
            case R.id.btn_need_receipt:
                isNeedReceipt = !isNeedReceipt;
                if (isNeedReceipt) {
                    btnNeedReceipt.setBackgroundResource(R.drawable.bg_extra_service_checked);
                } else {
                    btnNeedReceipt.setBackgroundResource(R.drawable.bg_extra_service_normal);
                }
                break;
            case R.id.btn_need_remittance:
                isNeedRemittance = !isNeedRemittance;
                if (isNeedRemittance) {
                    btnNeedRemittance.setBackgroundResource(R.drawable.bg_extra_service_checked);
                } else {
                    btnNeedRemittance.setBackgroundResource(R.drawable.bg_extra_service_normal);
                }
                break;
            case R.id.layout_tip_for_driver:
                //添加小费对话框
                new BillDialogFragment().show(getFragmentManager(),"bill_dialog");

                break;
            case R.id.layout_words_for_driver:
                intent.setClass(this, AddWordsActivity.class);
                startActivityForResult(intent, Constant.Order.REQUEST_CODE_5);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (RESULT_OK == resultCode) {
            //TODO
            switch (requestCode) {
                case Constant.Order.REQUEST_CODE_1:
                    int shippingType = data.getIntExtra(Constant.Order.SHIPPING_TIME_TYPE_KEY, Constant.Order.SHIPPING_TIME_TYPE_DAY);
                    //按天 的每周几
                    if (shippingType == Constant.Order.SHIPPING_TIME_TYPE_DAY) {
                        HashMap<Integer, Boolean> weeks = (HashMap) data.getSerializableExtra("weeks");
                        StringBuffer stringBuffer = new StringBuffer();
                        stringBuffer.append("每周");
                        String[] weekOfDays = {"一", "二", "三", "四", "五", "六", "日"};
                        for (int i = 1; i < 8; i++) {
                            if (weeks.get(i)) {
                                stringBuffer.append(weekOfDays[i]);
                            }
                        }
                        stringBuffer.append("发货");
                        tvTransportTimeDes.setText(stringBuffer.toString());
                    }
                    //按频率的 的每隔几天
                    if (shippingType == Constant.Order.SHIPPING_TIME_TYPE_PERIOD) {
                        int interval = data.getIntExtra("interval", 0);
                        tvTransportTimeDes.setText("每隔" + interval + "天发货");
                    }

                    String startDate = data.getStringExtra("start_date");
                    tvTransportDate.setText(startDate);

                    String transformTime = data.getStringExtra("transform_time");
                    break;
                case Constant.Order.REQUEST_CODE_2:
                    //新增空的配送地对象
                    Address address = data.getParcelableExtra("new_address");
                    int position = data.getIntExtra("position", 0);
                    addressList.set(position, address);
                    taskAddressAdapter.notifyDataSetChanged();
                    break;
                case Constant.Order.REQUEST_CODE_3:
                    //新增空的配送地对象
                    Address newAddress = data.getParcelableExtra("new_address");
                    addressList.add(newAddress);
                    taskAddressAdapter.notifyDataSetChanged();
                    break;
                case Constant.Order.REQUEST_CODE_4:
                    int truckType = data.getIntExtra(Constant.Order.KEY_TRUCK_TYPE, Constant.Order.VALUE_TRUCK_TYPE_0);
                    String truckStr = getResources().getStringArray(R.array.truck_title)[truckType];
                    tvChooseTruck.setText(truckStr);
                    break;
                case Constant.Order.REQUEST_CODE_5:
                    String words = data.getStringExtra("words");
                    tvWordsForDriver.setText(words);
                    break;
            }

        }
    }

    @Override
    public void onSureClick(String bill) {
        tvTipForDriver.setText(bill+"元");
    }
}
